﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI.WebControls;
using InventoryManagement;
using MII_FIF.Common;

namespace MII_FIF.Layouts.MII_FIF.Master.SystemType
{
    public partial class AddSystemType : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.MasterGroupName))
            {
                btnSubmit.Click += btnSubmit_Click;
                btnCancel.Click += btnCancel_Click;

                if (Request.QueryString["ID"] != null)
                    ((Label)Header.FindControl("lblHeader")).Text = "Edit System Type";
                else
                    ((Label)Header.FindControl("lblHeader")).Text = "Add System Type";

                if (!IsPostBack)
                {
                    if (Request.QueryString["ID"] != null)
                    {
                        ViewState["ID"] = Request.QueryString["ID"].ToString();
                    }
                    Initialize();
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void Initialize()
        {
            #region BindDDL
            System_Brand brand = new System_Brand();
            brand.IsDeleted = false;
            DataTable brandColl = brand.SelectByParam();
            Generic.BindDDL(ddlSystemBrand, brandColl, System_Brand_Fields.Title.ToString(), System_Brand_Fields.ID.ToString());
            #endregion

            if (Request.QueryString["ID"] != null)
            {
                InventoryManagement.System_Type IMType = new InventoryManagement.System_Type();
                IMType.ID = Guid.Parse(ViewState["ID"].ToString());
                IMType.Select();
                ddlSystemBrand.SelectedIndex = ddlSystemBrand.Items.IndexOf(ddlSystemBrand.Items.FindByValue(IMType.ID_SystemBrand.ToString()));
                txtSystemType.Text = IMType.Title;
                txtSize.Text = IMType.Size.Value.ToString();
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/SystemType/ViewSystemType.aspx"));
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            bool check = true;
            InventoryManagement.System_Type SystemType = new InventoryManagement.System_Type();
            int output = 0;
            if (txtSystemType.Text.Trim() == string.Empty)
            {
                check = false;
                lblError.Text += "Please Insert System Type <br />";
            }
            if (txtSize.Text.Trim() == string.Empty)
            {
                check = false;
                lblError.Text += "Please Insert Size <br />";
            }
            else if (!int.TryParse(txtSize.Text, out output))
            {
                check = false;
                lblError.Text += "Size must a number";
            }
            if (ddlSystemBrand.SelectedIndex == 0)
            {
                check = false;
                lblError.Text += "Please Select System Brand";
            }

            if (check)
            {
                SystemType.Title = txtSystemType.Text;
                SystemType.Size = int.Parse(txtSize.Text);
                SystemType.ID_SystemBrand = StringUtil.ToGuid(ddlSystemBrand.SelectedValue);
                SystemType.IsDeleted = false;
                if (Request.QueryString["ID"] == null)
                {
                    SystemType.ID = Guid.NewGuid();
                    SystemType.IsDeleted = false;
                    int stat = SystemType.Insert();
                    if (stat > 0)
                        Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/SystemType/ViewSystemType.aspx"));
                    else
                        lblError.Text += "Insert Failed";
                }
                else
                {
                    SystemType.ID = Guid.Parse(Request.QueryString["ID"].ToString());
                    SystemType.Select();
                    if (SystemType.IsDeleted.Value)
                    {
                        lblError.Text += "This item is already deleted <br />";
                    }
                    else
                    {
                        SystemType.Title = txtSystemType.Text;
                        SystemType.Size = int.Parse(txtSize.Text);
                        SystemType.ID_SystemBrand = StringUtil.ToGuid(ddlSystemBrand.SelectedValue);
                        SystemType.IsDeleted = false;
                        int stat = SystemType.Update();
                        if (stat > 0)
                            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/SystemType/ViewSystemType.aspx"));
                        else
                            lblError.Text += "Update Failed";
                    }
                }
            }
        }
    }
}
